﻿using Assets.Scripts.DL.Enemy.BaseClass.FSM;
using Assets.Scripts.DL.Enemy.CommonEnemy;
using Assets.Scripts.DL.Enemy.CommonEnemy.States.ATK;
using Assets.Scripts.DL.Enemy.CommonEnemy.States.CommonState;
using Assets.Scripts.DL.Enemy.CommonEnemy.States.Special;
using Assets.Scripts.DL.Enemy.CommonEnemy.States;
using Assets.Scripts.DL.Enemy.Wizard;
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.DL.Enemy.FlyEnemy {
	public class FlyEnemyController : Controller {
		public GameObject Player;

		protected override void Awake() {
			base.Awake();
			SetGravity(0);
			//Debug.Log(1);
		}

		protected override void InitAnimMgr() {
			//base.InitAnimMgr();
			AddAnim(StateEnum.Fly, "Fly");
			AddAnim(StateEnum.FlyDash, "Atk1");
			AddAnim(StateEnum.Atk2, "Atk2");
			AddAnim(StateEnum.Hurt, "Hurt");
			AddAnim(StateEnum.Die, "Die");
		}

		public override bool IsPlayerInAtkRange() {
			var findRes = Physics2D.OverlapCircle(transform.position, Config.ViewDis, GetLayer(LayerStrings.Player));
			if (findRes != null) {
				//Debug.Log("Find");
				Player = findRes.gameObject;
				return true;
			}
			return false;
		}

		public override Vector3 GetPlayerPos() {
			if (Player != null) {
				//Debug.Log(Player.transform.position);
				return Player.transform.position;
			}
			else { return Vector3.zero; }
		}

		protected override void InitStates() {
			//一般状态
			var commonHfsm = new CommonStateHFSM(this, StateEnum.CommonHFSM);
			var fly = new FlyMove(this, StateEnum.Fly);
			commonHfsm.InitSubStates(new Dictionary<StateEnum, EnemyState<StateEnum>> {
				{StateEnum.Fly, fly},
				}
			);

			//Atk
			var atkHFSM = new AtkHFSM(this, StateEnum.AtkHFSM);
			var dash = new FlyDash(this, StateEnum.FlyDash);
			dash.AddConditionToSelf(
					() => {
						var playerPos = GetPlayerPos();
						if (playerPos != null) {//若在近距离攻击范围则不dash
							return (playerPos - transform.position).sqrMagnitude > Mathf.Pow(Config.AtkDis * 2, 2);
						}
						return false;
					}
			);

			var atk2 = new Atk(this, StateEnum.Atk2);//近距离攻击
			atk2.AddConditionToSelf(
				() => {
					var playerPos = GetPlayerPos();
					if (playerPos != null) {
						return (playerPos - transform.position).sqrMagnitude < Mathf.Pow(Config.AtkDis * 2, 2);
					}
					return false;
				}
			);
			atkHFSM.InitSubStates(new Dictionary<StateEnum, EnemyState<StateEnum>> {
				{ StateEnum.FlyDash, dash },
				{ StateEnum.Atk2, atk2},
			});


			//Special
			var specialHFSM = new EnemySpecialHFSM(this, StateEnum.SpecialHFSM);
			var hurt = new Hurt(this, StateEnum.Hurt);
			var die = new Die(this, StateEnum.Die);
			specialHFSM.InitSubStates(new Dictionary<StateEnum, EnemyState<StateEnum>> {
				{ StateEnum.Die, die },{StateEnum.Hurt, hurt },
			}
			);

			//Root
			_rootHFSM.InitSubStates(new Dictionary<StateEnum, EnemyState<StateEnum>> {
				{StateEnum.CommonHFSM,  commonHfsm},
				{StateEnum.AtkHFSM,  atkHFSM},
				{StateEnum.SpecialHFSM, specialHFSM },
				}
			);
		}
		protected override void InitAtkTimer() {
			//base.InitAtkTimer();
			StateData.AtkCdRecoder = new Dictionary<StateEnum, Timer> {
				{StateEnum.Atk2, new Timer() },
				{StateEnum.Fly, new Timer() },
			};
		}

	}
}
